//长距离测试
//sml_move(400, 1800, 800, 230, 1000);
//短距离测试
//sml_move(400, 1000, 400, 300, 1000);
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    }
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    }

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    //log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    //log(xxy);
    gesture.apply(null, xxy);
}


//sml_move(400, 1800, 800, 230, 2000);

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
            break;
        default:
            return 0;
            break;
    }
}

function up() {
    let rNum = randomNum(100, 400);
    log("up");
    log("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down() {
    let rNum = randomNum(100, 400);
    let yNum = randomNum(950, 1000);
    log("down");
    log("x:" + rNum);
    log("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function up1(rNum) {
    log("up");
    log("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down1(rNum, yNum) {
    log("down");
    log("x:" + rNum);
    log("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function closeApp(packagename) {
    var sh = new Shell(true);
    //注册一个对象运用shell命令，true(真) 以root权限运行代码，默认为falae假
    // console.log("am force-stop"+" "+packagename)
    sh.exec("am force-stop" + " " + packagename); //执行代码运行中属于异步运行
    //上面值com.android.browser是浏览器的包名。自行修改成想停止软件的包名
    sleep(1000);
    //给点延迟让前面的运行命令，一会软件就会关闭
    sh.exit;
    //退出Shell命令，正在执行的命令会被强制退出。所以上面加延迟
    toast("关闭" + packagename);
}

function closeApk(packageName) {
    var name = getPackageName(packageName);
    if (!name) {
        if (getAppName(packageName)) {
            name = packageName;
        } else {
            return false;
        }
    }
    app.openAppSetting(name);
    text(app.getAppName(name)).waitFor();
    let is_sure = textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne();
    if (is_sure.enabled()) {
        textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne().click();
        textMatches(/(.*确.*|.*定.*)/).findOne().click();
        log(app.getAppName(name) + "应用已被关闭");
        sleep(1000);
        back();
    } else {
        log(app.getAppName(name) + "应用不能被正常关闭或不在后台运行");
        back();
    }
}

/**
 * 格式化时间函数
 * @param {format} 时间显示格式
 */
Date.prototype.format = function(format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
};


var p1 = 1000;
var p2 = 2000;
var p3 = 3000;
var p4 = 4000;
var p5 = 5000;

auto();
auto.waitFor();

console.setPosition(215, 400);
console.show();


let packageName = app.getPackageName("Zareklamy");
log(packageName);

function startApp() {
    closeApk(packageName);

    app.launchPackage(packageName);
    sleep(p5);
}

startApp();



begin();

function begin() {
    var count = 0;
    while (true) {
        log('count:', count);
        log("10秒");
        sleep(10000);
        click(262,446);
        sleep(2000);
        log("40秒");
        sleep(40000);
        click(655,45);
        log("10秒");
        sleep(10000);
        count++;
    };
};